package com.vms.gsmchecker;

import java.util.Vector;

import com.vms.data.GSMNetworkInfo;

import vms.data.ApplicationItem;
import vms.data.CheckerItem;
import vms.data.DataManager;
import vms.data.InternetItem;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;

public class ReportActivity extends Activity {
	public static final String HTML_SAMPLE_FULL = "<html><head><meta name=\"viewport\" content=\"width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=no;\" /></head><body><table border='1'>%s</table></body></html>";

	public static final int REPORT_GSM_NETWORK = 0;
	public static final int REPORT_DATA = 1;
	public static final int REPORT_LINK = 2;

	WebView webview;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		webview = new WebView(this);
		viewGSM();
		setContentView(webview);
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(1, REPORT_GSM_NETWORK, REPORT_GSM_NETWORK, "GSM Network");
		menu.add(1, REPORT_DATA, REPORT_DATA, "Internet (Data)");
		menu.add(1, REPORT_LINK, REPORT_LINK, "Application (Link)");
		// TODO Auto-generated method stub
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case REPORT_GSM_NETWORK:
			viewGSM();
			return true;
		case REPORT_DATA:
			viewData();
			return true;
		case REPORT_LINK:
			viewLink();
			return true;

		}
		return super.onOptionsItemSelected(item);
	}

	private void viewLink() {
		StringBuffer buff = new StringBuffer();
		buff.append("<tr><td>Time</td><td>CellID</td><td>LAC</td><td>RXL</td><td>Time (ms)</td><td>Link</td></tr>");
		Vector<ApplicationItem> l = DataManager.instance.getApplicationList();
		for (int index = 0; index < l.size(); index++) {
			ApplicationItem it = (ApplicationItem) l.elementAt(index);
			buff.append("<tr><td>"+it.getCheckTime() +"</td><td>"+GSMNetworkInfo.convertUTRAN(it.getCellid())+"</td><td>"+it.getLac()+"</td><td>"+it.getRxl()+"</td><td>"+it.getTime()+"</td><td>"+it.getLink()+"</td></tr>");
		}
		
		final String contents = String.format(HTML_SAMPLE_FULL, buff.toString());
		final String content = contents;
		Log.i("INTO", content);
		Handler refresh = new Handler(Looper.getMainLooper());
		refresh.post(new Runnable() {
			public void run() {
				webview.loadData(content, "text/html", "utf8");
			}
		});
	}

	private void viewData() {
		StringBuffer buff = new StringBuffer();
		buff.append("<tr><td>Time</td><td>CellID</td><td>LAC</td><td>RXL</td><td>Up (kb/s)</td><td>Down (kb/s)</td></tr>");
		Vector<InternetItem> l = DataManager.instance.getInternetList();
		for (int index = 0; index < l.size(); index++) {
			InternetItem it = (InternetItem) l.elementAt(index);
			buff.append("<tr><td>"+it.getCheckTime() +"</td><td>"+GSMNetworkInfo.convertUTRAN(it.getCellid())+"</td><td>"+it.getLac()+"</td><td>"+it.getRxl()+"</td><td>"+it.getUpspeed()+"</td><td>"+it.getDownspeed()+"</td></tr>");
		}
		
		final String contents = String.format(HTML_SAMPLE_FULL, buff.toString());
		final String content = contents;
		Log.i("INTO", content);
		Handler refresh = new Handler(Looper.getMainLooper());
		refresh.post(new Runnable() {
			public void run() {
				webview.loadData(content, "text/html", "utf8");
			}
		});
	}

	private void viewGSM() {
		StringBuffer buff = new StringBuffer();
		buff.append("<tr><td>Time</td><td>CellID</td><td>LAC</td><td>RXL</td></tr>");
		Vector<CheckerItem> l = DataManager.instance.getGMSList();
		for (int index = 0; index < l.size(); index++) {
			CheckerItem it = (CheckerItem) l.elementAt(index);
			buff.append("<tr><td>"+it.getCheckTime() +"</td><td>"+GSMNetworkInfo.convertUTRAN(it.getCellid())+"</td><td>"+it.getLac()+"</td><td>"+it.getRxl()+"</td></tr>");
		}
		
		final String contents = String.format(HTML_SAMPLE_FULL, buff.toString());
		final String content = contents;
		Log.i("INTO", content);
		Handler refresh = new Handler(Looper.getMainLooper());
		refresh.post(new Runnable() {
			public void run() {
				webview.loadData(content, "text/html", "utf8");
			}
		});
	}
}
